/**
 * UserAccountDAO.java
 * Nov 6, 2013
 * Copyright: phongtdse60732@fpt.edu.vn
 */
package DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import DBUltilities.ConnectDB;
import DTO.UserAccountDTO;

/**
 * @author trandophong
 * @version 1.0
 */
public class UserAccountDAO {

	public void add(UserAccountDTO user) {

		Connection conn = null;
		PreparedStatement preparedStatement = null;

		String sqlStatement = "INSERT INTO USERACCOUNT"
				+ "(USERNAME, PASSWORD, EMAIL) VALUES" + "(?,?,?)";

		try {
			conn = ConnectDB.getConnection();
			preparedStatement = conn.prepareStatement(sqlStatement);

			preparedStatement.setString(1, user.getUsername());
			preparedStatement.setString(2, user.getPassword());
			preparedStatement.setString(3, user.getEmail());

			preparedStatement.executeUpdate();
		} catch (SQLException e) {
		} finally {
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
				}
			}

		}
	}

	public void update(UserAccountDTO user) {

		Connection conn = null;
		PreparedStatement preparedStatement = null;

		String sqlStatement = "UPDATE USERACCOUNT SET PASSWORD = ?, EMAIL = ? WHERE USERNAME = ?";

		try {
			conn = ConnectDB.getConnection();
			preparedStatement = conn.prepareStatement(sqlStatement);

			preparedStatement.setString(3, user.getUsername());
			preparedStatement.setString(1, user.getPassword());
			preparedStatement.setString(2, user.getEmail());

			preparedStatement.executeUpdate();
		} catch (SQLException e) {
		} finally {
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
				}
			}

		}
	}

	public void delete(String username) {

		Connection conn = null;
		PreparedStatement preparedStatement = null;

		String sqlStatement = "DELETE FROM USERACCOUNT WHERE USERNAME = ?";

		try {
			conn = ConnectDB.getConnection();
			preparedStatement = conn.prepareStatement(sqlStatement);

			preparedStatement.setString(1, username);

			preparedStatement.executeUpdate();
		} catch (SQLException e) {
		} finally {
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
				}
			}
		}
	}

	public boolean checkAuthenticate(String username, String password) {
		Connection conn = null;
		PreparedStatement preparedStatement = null;

		String sqlStatement = "SELECT * FROM USERACCOUNT WHERE USERNAME = ? AND PASSWORD = ?";
		try {
			conn = ConnectDB.getConnection();
			preparedStatement = conn.prepareStatement(sqlStatement);

			preparedStatement.setString(1, username);
			preparedStatement.setString(2, password);

			return preparedStatement.executeQuery().next();

		} catch (SQLException e) {
		} finally {
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
				}
			}

		}
		return false;
	}

	public boolean checkUsernameExist(String username) {
		Connection conn = null;
		PreparedStatement preparedStatement = null;

		String sqlStatement = "SELECT * FROM USERACCOUNT WHERE USERNAME = ?";
		try {
			conn = ConnectDB.getConnection();
			preparedStatement = conn.prepareStatement(sqlStatement);

			preparedStatement.setString(1, username);

			return preparedStatement.executeQuery().next();

		} catch (SQLException e) {
		} finally {
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
				}
			}
		}
		return false;
	}

	public boolean checkEmailExist(String email) {
		Connection conn = null;
		PreparedStatement preparedStatement = null;

		String sqlStatement = "SELECT * FROM USERACCOUNT WHERE EMAIL = ?";
		try {
			conn = ConnectDB.getConnection();
			preparedStatement = conn.prepareStatement(sqlStatement);

			preparedStatement.setString(1, email);

			return preparedStatement.executeQuery().next();

		} catch (SQLException e) {
		} finally {
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
				}
			}
		}
		return false;
	}
}
